// pages/my/birthday.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    showMonment: true, //是否显示须知
    showGift: false,

    openid: '',
    shareTitle: "",
    shareImg: '',
    flag: 0,
    prizeInfo: [],
    ruleImg:''
  },
  //点击我知晓了
  closeMonment() {
    let that = this;
    that.setData({
      showMonment: false
    })
  },
  showMonment() {
    let that = this;
    that.setData({
      showMonment: true
    })
  },
  //点击抽奖
  cj() {
    var that = this;
    if (that.data.flag == 0) {
      that.setData({
        flag: 1
      });
      //后台请求抽奖
      wx.request({
        url: that.data.baseUrl + 'getMyBirthDayPrize.php',
        method: 'POST',
        data: {
          openid: that.data.openid
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          if (res.data.status == 1) {
            that.videoContext.play();
            that.setData({
              prizeInfo: res.data.prizeInfo
            });
          } else {
            app.showTip(res.data.msg);
          }
          that.setData({
            flag: 0
          });
        },
        fail: function(res) {
          app.showTip('服务器繁忙,请稍候重试');
          that.setData({
            flag: 0
          });
        }
      });
    }
  },
  videoEnd() {
    this.setData({
      showGift: true,
      flag: 0
    })
  },
  goBirthdayOrder() {
    wx.redirectTo({
      url: '/pages/my/birthdayOrder'
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;


    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function(res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function(res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getMyBirthDayPageInfo(res.data.openid);
              },
              fail: function(res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getMyBirthDayPageInfo(openid);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getMyBirthDayPageInfo(openid) {
    var that = this;

    wx.showLoading({
      title: '数据加载中...'
    });
    wx.request({
      url: that.data.baseUrl + 'getMyBirthDayPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //重新赋值后台请求数据
          //$isStatus=0 不显示
          //$isStatus=1 显示 但未抽奖
          //$isStatus=2 显示 已抽奖
          //$isStatus=3 不显示 过期
          //$isStatus=4 已兑换
          that.setData({
            ruleImg:res.data.ruleImg
          });

        } else if (res.data.status == 100) {
          //已抽过奖跳转至预约页面
          wx.redirectTo({
            url: '/pages/my/birthdayOrder'
          });
        } else if (res.data.status == 200) {
          //已经预约过了或者已经核销过了 跳转至成功页面
          wx.redirectTo({
            url: '/pages/my/birthdayOrderResult'
          });
        } else {
          app.showTip(res.data.msg);
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function() {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function() {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function(res) {}
    });
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.videoContext = wx.createVideoContext('video1')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function(res) {

      },
      fail: function(res) {

      }
    }
  }
})